<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 客户
 *
 *
 */
class Customer_model extends CI_Model
{

    var $name;
	var $password;
	var $birthday;
	var $firstName;
	var $lastName;
	var $city;
	var $sex;

	function __construct()
    {
        parent::__construct();
    }

	// --------------------------------------------------------------------

    /**
	 * 添加新客户
	 *
	 *
	 */	
	function create_custom_login_info()
    { 
		$datetime = mysql_datetime();
        $this->db->set('nickname', $this->name);
		$this->db->set('password', md5($this->password));
		$this->db->set('lastloginat', $datetime);
        $this->db->insert('userlogin');
   
    }

    /**
     * 添加新客户信息
     *
     *
     */
    function create_custom_base_info($id)
    {
    	$datetime = mysql_datetime();
    	$this->db->set('uid', $id);
    	$this->db->set('firstName', $this->firstName);
    	$this->db->set('lastName', $this->lastName);
    	$this->db->set('city', $this->city);
    	$this->db->set('sex', $this->sex);
    	//TODO
    	//$this->db->set('birthday', $this->birthday);
    	$this->db->set('birthday', $datetime);
    	
    	$this->db->set('regDate', $datetime);
    	$this->db->insert('userbaseinfo');
    }
    
    // --------------------------------------------------------------------

    /**
	 * 查询该用户名是否存在
	 *
	 *
	 */	
	function check_name($name)
	{
		$query = $this->db->get_where('userlogin',array('nickname' => $name));
        if ($row = $query->row_array()){
            return true;
        }
        return false;
	}
   
   // --------------------------------------------------------------------

    /**
	 * 查询该用户，返回用户信息
	 *
	 *
	 */	
	function check_customer()
	{
        $query = $this->db->get_where('userlogin',array('nickname' => $this->name,'password' => md5($this->password)));
        if ($row = $query->row_array()){			
            return $row;
        }
        return array();
	}
	
	/**
	 * 检查管理员信息
	 */
	function check_admin()
	{
		$query = $this->db->get_where("admin", array(
				"account" => $this->name,
				"password" => md5($this->password)
				));
		
		if($row = $query->row_array())
		{
			return $row;
		}
		
		return array();
	}

	// --------------------------------------------------------------------

    /**
	 * load by id
	 *
	 *
	 */	
    function load($id)
    {
        if (!$id){
            return array();
        }

        $query = $this->db->get_where('userlogin',array('uid' => $id));

        if ($row = $query->row_array()){
            return $row;
        }

        return array();
    }
	
    // --------------------------------------------------------------------

    /**
	 * 更新客户信息
	 *
	 *
	 */	
	function update($id)
    {
        //$datetime = date('Y-m-d H:i:s');
		//$this->db->set('updated_at', $datetime);
		
        $this->db->where('uid', $id);
        return $this->db->update('userbaseinfo');
    }

    // --------------------------------------------------------------------

    /**
	 * 查询密码是否正确
	 *
	 *
	 */	
    function check_pwd($password)
	{
		$query = $this->db->get_where('userlogin',array('password' => md5($password)));
        if ($row = $query->row_array()){
            return true;
        }
        return false;
	}
    
	// --------------------------------------------------------------------

    /**
	 * 更新密码
	 *
	 *
	 */	
	function update_pwd($id,$pwd)
    {
		$this->db->set('password', md5($pwd));	
        $this->db->where('id', $id);
        return $this->db->update('userlogin');
    }
    
	// --------------------------------------------------------------------

    /**
	 * 更新用户最后登录时间
	 *
	 *
	 */	
    function update_user_last_login($customer_id)
	{
		$datetime = mysql_datetime();
		$this->db->set('lastloginat', $datetime);	
        $this->db->where('uid', $customer_id);
        return $this->db->update('userlogin');
	}
	
	function get_last_login($customer_id)
	{
		$query = $this->db->get_where('userlogin',array('uid' => $customer_id));
		$row = $query->row();
		return $row->lastloginat;
	}
	
	/**
	 * 更新管理员最后登录时间
	 *
	 *
	 */
	function update_admin_last_login($admin_id)
	{
		$datetime = mysql_datetime();
		$this->db->set('lastloginat', $datetime);
		$this->db->where('aid', $admin_id);
		return $this->db->update('admin');
	}
	
	public function get_base_info($uid)
	{
		$this->db->where("uid", $uid);
		$query = $this->db->get("userbaseinfo");
		
		if(!$query->num_rows())
			return null;
		
		return $query->row();
	}
	
	public function get_all_friend_symptom($uid)
	{
		
	}
}
